package cn.wyz.basic.controller;

import cn.wyz.basic.dto.RegisterDto;
import cn.wyz.basic.exception.BusinessException;
import cn.wyz.basic.service.IVerifyCodeService;
import cn.wyz.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 图形验证码的接口
     * this.$http.get("/verifyCode/image/"+imageCodeKey)
     * @param imageCodeKey
     * @return
     */
    @GetMapping("/image/{imageCodeKey}") //所有要获取图片验证码都可以使用
    public AjaxResult imgCode(@PathVariable("imageCodeKey") String imageCodeKey){
        try {
            String base64code = verifyCodeService.getImgCode(imageCodeKey);
            return AjaxResult.make().setResultObj(base64code);
        } catch (BusinessException e) { //这是我们自定义的业务异常,需要我们自己去处理
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.make();
        } catch (BusinessException e) { //这是我们自定义的业务异常,需要我们自己去处理
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
}
